<%@ taglib prefix="t" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: TZZ
  Date: 2020/5/15
  Time: 17:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <%@include file="/bootstrap.jsp" %>
    <style type="text/css">
        .cz button {
            margin: 10px 20px;
        }

        .sub {
            border: 1px solid red;
        }
    </style>
</head>

<body>

<!-- 按钮触发模态框 -->
<div class="cz bg-info">
    <button type="botton" class="btn btn-primary" data-toggle="modal" data-target="#addModal">添加新闻</button>
</div>

<!-- 添加模态框 -->
<div class="modal fade" id="addModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <!-- 模态框头部 -->
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            </div>
            <!-- 模态框主体 -->
            <div class="modal-body">
                <form class="bs-example bs-example-form" role="form"
                      action="${pageContext.request.contextPath}/NewsAddServlet.filter">
                    <div class="form-group">
                        <label for="addTitle">标题:</label>
                        <input type="text" class="form-control" placeholder="标题" id="addTitle" name="n_title">
                    </div>
                    <br>
                    <div class="form-group">
                        <label for="addName">作者:</label>
                        <input type="text" class="form-control" placeholder="作者名称" id="addName" name="u_name" value="${user.u_name}">
                    </div>
                    <br>
                    <%--下拉菜单--%>
                    <div>
                        <label for="addType">类型:</label>
                        <select class="form-control" id="addType" name="t_name">
                            <t:forEach items="${list[0]}" var="type">
                                <option value="${type.t_name}">${type.t_name}</option>
                            </t:forEach>
                        </select>
                    </div>
                    <br>
                    <%--新闻内容--%>
                    <div class="form-group">
                        <label for="addContent">正文:</label>
                        <textarea class="form-control" rows="5" id="addContent" name="n_content"></textarea>
                    </div>
                    <br>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                        </button>
                        <button type="submit" class="btn btn-primary">
                            提交
                        </button>
                    </div>
                </form>

            </div>
        </div>
    </div>
</div>

<%--新闻列表--%>
<table class="table-hover table-striped table-bordered table">
    <thead class="table-dark">
    <tr>
        <td>编号</td>
        <td>新闻标题</td>
        <td>作者</td>
        <td>类型</td>
        <td>内容</td>
        <td>上传时间</td>
        <td>更新时间</td>
        <td>操作</td>
    </tr>
    </thead>
    <tbody>
    <t:forEach items="${list[1]}" var="news">
        <tr>
            <td>${news.n_id}</td>
            <td>${news.n_title}</td>
            <td>${news.u_name}</td>
            <td>${news.t_name}</td>
            <td>${news.n_content}</td>
            <td>${news.create_time}</td>
            <td>${news.update_time}</td>
            <td>
                    <%--<a type="button" class="btn btn-danger" href="${pageContext.request.contextPath}/NewsUpdateServlet.filter?n_id=${news.n_id}"
                           style="color: white;text-decoration: none">发布</a>--%>
                <a type="button" class="btn btn-danger" onclick="deleteNews('${news.n_id}')"
                   style="color: white;text-decoration: none">删除</a>
                <a class="btn btn-info" type="button" data-toggle="modal" data-target="#updateModal"
                   onclick="updateNews('${news.n_id}','${news.n_title}','${news.u_name}','${news.t_name}','${news.n_content}')"
                   style="color: white;text-decoration: none">修改</a>
            </td>
        </tr>
    </t:forEach>
    </tbody>
</table>


<script type="text/javascript">
    function updateNews(n_id, n_title, u_name, t_name, n_content) {
        document.getElementById("updateId").value = n_id;
        document.getElementById("updateTitle").value = n_title;
        document.getElementById("updateName").value = u_name;
        // document.getElementById("updateType").value = t_name;
        document.getElementById("updateContent").value = n_content;
    }
    function deleteNews(n_id) {
        var con = confirm("是否删除?");
        if (con) {
            location.href = "${pageContext.request.contextPath}/NewsDeleteServlet.filter?n_id=" + n_id;
        }
    }
</script>


<%--修改模态框--%>
<div class="modal fade" id="updateModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div style="padding: 10px;">
                <%--关闭按钮--%>
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                </div>
                <form class="bs-example bs-example-form" role="form"
                      action="${pageContext.request.contextPath}/NewsUpdateServlet.filter">
                    <%--新闻编号--%>
                    <div class="form-group">
                        <label for="updateId">新闻编号:</label>
                        <input type="text" class="form-control" placeholder="新闻编号" id="updateId" name="n_id" value="">
                    </div>
                    <br>
                    <%--新闻标题--%>
                    <div class="form-group">
                        <label for="updateTitle">新闻标题:</label>
                        <input type="text" class="form-control" placeholder="新闻标题" id="updateTitle" name="n_title"
                               value="">
                    </div>
                    <br>
                    <%--作者--%>
                    <div class="form-group">
                        <label for="updateName">作者:</label>
                        <input type="text" class="form-control" placeholder="类型编号" id="updateName" name="u_name"
                               value="">
                    </div>
                    <%--下拉菜单--%>
                    <div class="form-group">
                        <label for="updateName">类型:</label>
                        <select class="form-control" id="sel1" name="t_name">
                            <t:forEach items="${list[0]}" var="type">
                                <option value="${type.t_name}" >${type.t_name}</option>
                            </t:forEach>
                        </select>
                    </div>

                    <%--新闻内容--%>
                    <div class="form-group">
                        <label for="updateContent">正文:</label>
                        <textarea class="form-control" id="updateContent" rows="3" name="n_content">
                        </textarea>
                    </div>
                    <br>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">
                            关闭
                        </button>
                        <button type="submit" class="btn btn-primary">
                            提交
                        </button>
                    </div>
                </form>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
</body>
</html>
